package sv.com.renacempleo.dao.ctg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.renacempleo.dao.BaseDaoImpl;
import sv.com.renacempleo.dto.ctg.CtgDocumentos;
import sv.com.renacempleo.exception.WebException;

public class CtgDocumentosDaoImpl extends BaseDaoImpl implements CtgDocumentosDao{

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllActivesAsArray() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgDocumentos.class);
		criteria.addOrder(Order.asc("ctgDocumentoId"));
		criteria.addOrder(Order.asc("ctgDocumentoNombre")); 
		criteria.add(Restrictions.eq("ctgDocumentoEstado", "1"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgDocumentoId")).
				add(Projections.property("ctgDocumentoNombre"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllAsArray() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgDocumentos.class);
		criteria.addOrder(Order.asc("ctgDocumentoId"));
		criteria.addOrder(Order.asc("ctgDocumentoNombre")); 
		criteria.add(Restrictions.eq("ctgDocumentoEstado", "1"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgDocumentoId")).
				add(Projections.property("ctgDocumentoNombre")).
				add(Projections.property("ctgDocumentoEstado")).
				add(Projections.property("ctgDocumentoDescripcion")).
				add(Projections.property("ctgDocumentoIdentificacion")).
				add(Projections.property("ctgDocumentoVigente"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllActiveByIdentificacion() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgDocumentos.class);
		criteria.addOrder(Order.asc("ctgDocumentoId"));
		criteria.addOrder(Order.asc("ctgDocumentoNombre")); 
		criteria.add(Restrictions.eq("ctgDocumentoEstado", "1"));
		criteria.add(Restrictions.eq("ctgDocumentoIdentificacion", "1"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgDocumentoId")).
				add(Projections.property("ctgDocumentoNombre"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllActiveByVigencia() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgDocumentos.class);
		criteria.addOrder(Order.asc("ctgDocumentoId"));
		criteria.addOrder(Order.asc("ctgDocumentoNombre")); 
		criteria.add(Restrictions.eq("ctgDocumentoEstado", "1"));
		criteria.add(Restrictions.eq("ctgDocumentoVigente", "1"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgDocumentoId")).
				add(Projections.property("ctgDocumentoNombre"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@Override
	public void delete(Long ctgDocumentoId) throws WebException {
		try {
			String hqlQuery = "delete from CtgDocumentos where ctgDocumentoId = :ctgDocumentoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgDocumentoId", ctgDocumentoId);
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (Exception e) {
			throw new WebException("Problemas al tratar de eliminar el registro <br />Registro puede estar asociado a un Demandantes, etc");
		}
		
	}

	@Override
	public void toggleActive(Long ctgDocumentoId, String ctgDocumentoEstado)
			throws WebException {
		try {
			String hqlQuery = "update CtgDocumentos set ctgDocumentoEstado = :ctgDocumentoEstado where ctgDocumentoId = :ctgDocumentoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgDocumentoId", ctgDocumentoId);
			parameters.put("ctgDocumentoEstado", "1".equals(ctgDocumentoEstado) ? "0" : "1");
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (WebException e) {
			throw e;
		} catch (Exception e) {
			throw new WebException("Problemas al actualizar la informaci\u00F3n");
		}
		
	}

}
